<?php

namespace app\common\model;

use think\Db;
use think\Model;


class ChainFunds extends Model
{

    

    

    // 表名
    protected $name = 'chain_funds';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
		'type_text'
    ];
    

    const TYPE_SELF = 1;
    const TYPE_SON = 2;
    const TYPE_DESTORY = 3;

	public static $typeList = [
		self::TYPE_SELF => '提货点销售',
		self::TYPE_SON => '提货点代言人销售',
		self::TYPE_DESTORY => '提货点核销'
	];

	public function getTypeTextAttr($value, $data) {
		$value = $value ? : $data['type'];
		$list = self::$typeList;
		return $list[$value] ?? '';
	}
	
	
	public function user() {
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function agent() {
		return $this->belongsTo('agentShop', 'agent_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	
	/**
	 * 添加记录
	 * @param $userId 归属用户
	 * @param $agentId 提货点ID
	 * @param $orderId 订单ID
	 * @param $amount 金额
	 * @param $type 类型
	 * @return int|string
	 */
	public static function addRecord($userId, $agentId, $orderId, $amount, $type){
		return Db::name('chainFunds')->insert([
			'user_id' => $userId,
			'agent_id' => $agentId,
			'order_id' => $orderId,
			'amount' => $amount,
			'type' => $type,
			'create_time' => time(),
			'update_time' => time()
		]);
	}

}
